package gis.lifay.sdk;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import hdf.object.Attribute;
import hdf.object.h4.H4SDS;
import hdf.view.Tools;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

public class Main {
    private static String fname = "D:/hdf5/GLASS02B06.V04.A2000001.2017011.hdf";
    //private static String fname = "D:/hdf5/GLASS09B02.V04.A1982001.2018240.hdf";
    private static String lutPath = System.getProperty("user.dir") + File.separator + "new.lut";

    public static void main(String[] args) throws Exception {
        //testTile();
        //testHdf();
        // testImg();
        testShortImg();
    }

    private static void testShortImg() throws Exception {
        long s = System.currentTimeMillis();
        File oldfile = new File(System.getProperty("user.dir") + File.separator + "H4GRImage_bytespng.png");
        File newfile = new File(System.getProperty("user.dir") + File.separator + "shortImg.png");
        int w = 720;
        int h = 360;
        BufferedImage bis = ImageIO.read(oldfile);
        BufferedImage bid = new BufferedImage(w, h, 1);
        Graphics2D g = bid.createGraphics();
        //将读取原文件 写在目标文件的 0,0  w h 范围
        g.drawImage(bis, 0, 0, w, h, null);
        ImageIO.write(bid, "png", new FileOutputStream(newfile));
        g.dispose();


        long e = System.currentTimeMillis();
        System.out.println(e - s);
    }

    private static void testImg() throws Exception {
        long s = System.currentTimeMillis();
        HdfModel hdfModel = new HdfModel(fname);
        hdfModel.open();
        Image image = hdfModel.convertImage(hdfModel.getAllSubDataSets().get(0), lutPath);
        Tools.saveImageAs((BufferedImage) image, new File(System.getProperty("user.dir") + File.separator + "H4GRImage_bytespng.png"), "png");
        hdfModel.close();
        long e = System.currentTimeMillis();
        System.out.println(e - s);
    }

    private static void testHdf() throws Exception {
        long s = System.currentTimeMillis();
        HdfModel hdfModel = new HdfModel(fname);
        hdfModel.open();
        Map<String, H4SDS> allSubDataSets = hdfModel.getAllSubDataSets();
        H4SDS h4SDS = allSubDataSets.values().stream().findFirst().get();
        h4SDS.read();
        DatasetProperties hdfProperties = BeanUtil.copyProperties(h4SDS, DatasetProperties.class);
        hdfProperties.setFilename(h4SDS.getName());
        hdfProperties.setPath(h4SDS.getPath());
        List<Attribute> metadata = h4SDS.getMetadata();
        hdfProperties.setMetadatas(metadata);
        //Image image = hdfModel.convertImage(allSubDataSets.get(0), (String) null);
        hdfModel.close();
        System.out.println(JSON.toJSONString(hdfProperties));
        long e = System.currentTimeMillis();
        System.out.println(e - s);
    }

    private static void testTile() throws Exception {
        long s = System.currentTimeMillis();
        List<H4SDS> dataSets = HDFUtil.getAllSubDataSets(fname, false);
        for (H4SDS dataSet : dataSets) {
            Image image = HDFUtil.convertImage(dataSet, System.getProperty("user.dir") + File.separator + "gray.lut");
            hdf.view.Tools.saveImageAs((BufferedImage) image, new File("H4GRImage_bytesNature.jpg"), "jpeg");
            break;
        }
        long e = System.currentTimeMillis();
        System.out.println(e - s);
    }
}
